<template>
  <div>
    <el-table :data="goods">
      <el-table-column prop="name" label="商品名称" width="500"></el-table-column>
      <el-table-column prop="category.name" label="所属分类" width></el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button
            @click="$router.push(`/goods/edit/${scope.row._id}`)"
            type="text"
            size="small"
          >编辑</el-button>
          <el-button @click="remove(scope.row._id, scope.row.name)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      goods: []
    };
  },
  methods: {
    async fetch() {
      await this.$http.get("rest/categories");
      const res = await this.$http.get("rest/goods");
      this.goods = res.data;
    },
    remove(id, name) {
      this.$confirm(`是否删除："${name}"`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(async () => {
        await this.$http.delete(`rest/goods/${id}`);
        this.$message({
          message: "商品删除成功",
          type: "success"
        });
        this.fetch();
      });
    }
  },
  created() {
    this.fetch();
  }
};
</script>